The Zip Components
The Zip and Unzip components provide a ColdFusion API for zipping and unzipping either files on your server's hard drive or files that have been read into variables using CFFILE. Use the Zip component to compress individual files and folders, or recursively compress all files in any number of folders. Use the Unzip component either to expand an existing .zip file into variables representing each entry or to save the entries in a specified location on your server's hard drive.
Installing the Zip Components
The Zip package contains three components: Zip, Unzip, and ZipUtils. You can put them anywhere that your server finds components (typically somewhere in your webroot) and in any package you want as long as all three are together. I keep them in the following package:
com.macromedia.util.zip
That package translates into the following folder structure:
yourWebRoot/com/macromedia/util/zip/Zip.cfc
yourWebRoot/com/macromedia/util/zip/Unzip.cfc
yourWebRoot/com/macromedia/util/zip/ZipUtils.cfc
Using the structure above, I instantiate one of the Zip components like this:
<cfset zip = createObject("component", "com.macromedia.util.zip.Zip")/>
Note: You can also place the component in a folder under a ColdFusion mapping or in a custom tag root rather than in your webroot, if you prefer.
Notice that there are no .class or .jar files to install on the server. One of the goals of the Zip components was to make them as self-contained, platform-independent, and easy to install and use as possible. Because ColdFusion MX 6.1 is implemented entirely in Java, I was able to write both the Zip and Unzip components purely in Java using CFScript; no additional files or extensions are needed. This also means that the Zip components are ideal for ColdFusion developers who work in server environments where they are not allowed to add files to the server's classpath.
Using the Zip Components
The Zip and Unzip components are very easy to use. (ZipUtils is a utility component used by Zip.cfc and Unzip.cfc; you don’t use it directly.) To compress a set of files, start by creating an instance of the Zip component:<cfset zip = createObject("component", "path.to.Zip")/>
Add a file from disk. The first argument is the name (the name used to save the file when it is uncompressed, including folders) and the second argument is the path to the file:
<cfset zip.addFilePath("directory/myfile.doc", "path/to/myfile.doc")/>
Now recursively add a folder. All folders inside the folder are added automatically, as well. The names of the entries in the .zip file are derived from a combination of the name passed in and the name of the files on disk, including folder names:
<cfset zip.addFilePath("directory/myDirectory", "path/to/myDirectory")/>
Use CFFILE to load a file and then add it. The action attribute of the CFFILE tag must be readBinary, even if the file is text because the second argument to the addEntry function must be a byte array:
<cffile action="readBinary" file="path/to/myFile.txt" variable="myFile"/
<cfset zip.addEntry("directory/myfile.txt", myFile)/>
Now add the content of a variable to the .zip file:
<cfset myVar = "This will become its own file."/>
<cfset zip.addEntry("directory/myNewFile.txt", myVar.getBytes())/>
Get the .zip file as a byte array, which you can save using CFFILE:
<cfset myZipFile = zip.getZipFile()/> <cffile action="write" file="file/to/save.zip" output="#myZipFile#"/>
You can also have the Zip component save the file for you:
<cfset zip.saveZipFile("file/to/save.zip")/>
Uncompressing files is equally easy. Start by creating an instance of the Unzip component:
<cfset unzip = createObject("component", "path.to.Unzip")/>
Load a .zip file from disk:
<cfset unzip.setZipFilePath("path/to/zip/file.zip")/>
Alternatively, you can load the .zip file using CFFILE and pass in the bytes:
<cffile action="readBinary" file="path/to/zip/file.zip" variable="byteArray"/> <cfset unzip.setFileBytes(byteArray)/>
Get the .zip file entries (the files contained in the .zip file) as a structure with the properties compressedSize, crc, name, uncompressedSize, modificationTime, isDirectory, and bytes (which contains the actual bytes of the entry):
<cfset zipEntries = unzip.getEntries()/>
You can also tell the Unzip component to save the entries for you:
<cfset unzip.saveEntries("path/where/entries/will/be/saved")/>
Take a look at each of the components' documentation (inside the components themselves) for more information on how you can use them.
How the Zip Components Work
The Zip components leverage the java.util.zip package that is available to all instances of ColdFusion MX 6.1. The Zip package contains special streams that know how to write to and read from .zip files. The Zip components simply provide an interface between ColdFusion and the underlying JVM.
Although there are a number of ColdFusion-based compression utilities out there, the Zip package on DevNet Resource Kit Volume 6 is the easiest to install and use. You can use it on any platform and install it on even the most restrictive of server environments. Finally, the API is flexible enough to allow you to work with the components in a variety of different ways.